//
//  MineFuncVMNew.swift
//  kxf
//
//  Created by LNS2 on 2024/4/21.
//

import Foundation
import UIKit

enum MINE_FUNC_TYPE {
    case tools
    case merc_msg
    case bankcard
    case trade_detail
    case help_center
    case setting
    case merc_manage
    case settle_manage
    case authori
    case merc_fee
    case merc_sign
}

class MineFuncVMNew: UIView {
    
    var selfHeight = kFitWidth(288)
    
    var tapBlock:((MINE_FUNC_TYPE)->())?
    
    lazy var dataArray: NSArray = {
        return [["name":"商户授权","img":"mine_func_authori","type":"func_merc_authori"],
                ["name":"商户签约","img":"mine_func_sign","type":"func_merc_sign"],
                ["name":"收款工具","img":"mine_func_shoukuan","type":"func_tools"],
                ["name":"商户信息","img":"mine_func_merc_msg","type":"func_merc_msg"],
                ["name":"结算卡号","img":"mine_func_bank","type":"func_bankcard"],
                ["name":"交易明细","img":"mine_func_trade_detail","type":"func_trade_detail"],
//                ["name":"结算管理","img":"mine_func_merc_manage","type":"func_settle_manage"],
                ["name":"店铺管理","img":"mine_func_merc_manage","type":"func_merc_manage"],
                ["name":"商户费率","img":"mine_func_handfee","type":"func_merc_fee"],
                ["name":"帮助中心","img":"mine_func_service","type":"func_help_center"],
                ["name":"我的设置","img":"mine_func_settting","type":"func_setting"]]
    }()
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: kFitWidth(0), y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        self.layer.cornerRadius = kFitWidth(35)
        self.clipsToBounds = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var whiteView : UIView = {
        let vi = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: self.frame.size.height))
        vi.backgroundColor = .white
        vi.layer.cornerRadius = kFitWidth(35)
        vi.clipsToBounds = true
        
        return vi
    }()
}

extension MineFuncVMNew{
    func initUI() {
        addSubview(whiteView)
        
    }
    func updateUI() {
        let arr = NSMutableArray.init(array: dataArray)
        
        for vi in whiteView.subviews{
            vi.removeFromSuperview()
        }
        
        for i in 0..<dataArray.count{
            let index = dataArray.count-1-i
            let dic = arr[index]as? NSDictionary ?? [:]
            
            if dic["type"]as? String ?? "" == "func_merc_fee" && UserModel.shared.isShowRate != "Y" {
                arr.removeObject(at: index)
                continue
            }else if dic["type"]as? String ?? "" == "func_merc_authori"{
                if UserModel.shared.posStatus == "2" || UserModel.shared.posStatus == "3" || UserModel.shared.posStatus == "4"{
                    
                }else{
                    arr.removeObject(at: index)
                    continue
                }
            }else if dic["type"]as? String ?? "" == "func_merc_sign"{
                if UserModel.shared.posStatus == "1" || UserModel.shared.posStatus == "2" || UserModel.shared.posStatus == "3" || UserModel.shared.posStatus == "4"{
                    if UserModel.shared.signType != "" && UserModel.shared.signType != "1" && UserModel.shared.signType != "2"{
                        
                    }else{
                        arr.removeObject(at: index)
                        continue
                    }
                }else{
                    arr.removeObject(at: index)
                    continue
                }
            }
            
            if UserModel.shared.isCheck{
                if dic["type"]as? String ?? "" == "func_merc_fee"{
                    arr.removeObject(at: index)
                }else if dic["type"]as? String ?? "" == "func_merc_manage"{
                    arr.removeObject(at: index)
                }else if dic["type"]as? String ?? "" == "func_settle_manage"{
                    arr.removeObject(at: index)
                }else if dic["type"]as? String ?? "" == "func_merc_authori"{
                    arr.removeObject(at: index)
                }else if dic["type"]as? String ?? "" == "func_merc_sign"{
                    arr.removeObject(at: index)
                }
            }
        }
        
        selfHeight = kFitWidth(54) + CGFloat((arr.count + 3) / 4) * MineFuncItemVMNew().selfHeight
        
        let selfFrame = self.frame
        self.frame = CGRect.init(x: 0, y: selfFrame.origin.y, width: SCREEN_WIDHT, height: selfHeight)
        whiteView.frame = CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: selfHeight)
        
        var originX = kFitWidth(8)
        var originY = kFitWidth(14)
        for i in 0..<arr.count{
            originX = kFitWidth(8) + CGFloat(i % 4) * MineFuncItemVMNew().selfWidth
            originY = kFitWidth(14) + CGFloat(i / 4) * MineFuncItemVMNew().selfHeight
            
            let vm = MineFuncItemVMNew.init(frame: CGRect.init(x: originX, y: originY, width: 0, height: 0))
            whiteView.addSubview(vm)
            
            let dict = arr[i]as? NSDictionary ?? [:]
            vm.imgView.setImgLocal(imgName: dict["img"]as? String ?? "")
            vm.contentLabel.text = dict["name"]as? String ?? ""
            vm.typeString = dict["type"]as? String ?? ""
            vm.tapBlock = {(typeString)in
                var type = MINE_FUNC_TYPE.tools
                
                switch typeString {
                case "func_tools":
                    type = .tools
                case "func_merc_msg":
                    type = .merc_msg
                case "func_bankcard":
                    type = .bankcard
                case "func_trade_detail":
                    type = .trade_detail
                case "func_help_center":
                    type = .help_center
                case "func_setting":
                    type = .setting
                case "func_merc_manage":
                    type = .merc_manage
                case "func_merc_authori":
                    type = .authori
                case "func_merc_fee":
                    type = .merc_fee
                case "func_merc_sign":
                    type = .merc_sign
                case "func_settle_manage":
                    type = .settle_manage
                default:
                    
                    break
                }
                if self.tapBlock != nil{
                    self.tapBlock!(type)
                }
            }
        }
    }
}
